package io.guangli.tutorials1v1vcall.httpcomponent

import android.content.Context
import android.util.Log
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.lzy.okgo.request.base.Request
import json

/**
 * Created by Administrator on 2017/7/25.
 */
abstract class OkGoStringCallBack<S>(
    val context: Context,
    val clazz: Class<S>,
    val openDialog: Boolean = true
) :
    StringCallback() {

    override fun onStart(request: Request<String, out Request<Any, Request<*, *>>>?) {
        super.onStart(request)

    }

    override fun onSuccess(response: Response<String>?) {
        if (null == response) {
            onError(null)
            return
        }
        Log.e("解析结果", response?.body().toString())
        val bean = json(context, response!!.body(), clazz)
        if (null != bean) {
            onSuccess2Bean(bean)
        } else {
            response.exception = IllegalStateException("json 解析出错---返回---${response.body()}")
            onError(response)
        }
    }

    abstract fun onSuccess2Bean(bean: S?)

    override fun onFinish() {
        super.onFinish()
    }

    override fun onError(response: Response<String>?) {
        super.onError(response)
        if (null != response) {

        } else {
            Log.e("OkGoStringCallBack", "请求出错---返回null")
        }
    }


}